﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LGE;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Audio;

namespace Thaneros
{
    public class PlayScreen : GameScreen
    {
        public BlockManager bm;
        public Character c;
        public List<Enemy> enemies;

        // XACT
        public AudioEngine audioEngine;
        public WaveBank waveBank;
        public SoundBank soundBank;
        AudioCategory bgmCategory;
        AudioCategory sfxCategory;
        float bgmVolume = 1f;
        float sfxVolume = 1f;

        public PlayScreen()
            : base()
        {
            enemies = new List<Enemy>();
        }

        public override void LoadContent()
        {
            audioEngine = new AudioEngine("Content\\Audio\\Thaneros.xgs");
            waveBank = new WaveBank(audioEngine, "Content\\Audio\\Music.xwb");
            soundBank = new SoundBank(audioEngine, "Content\\Audio\\Music.xsb");
            soundBank.PlayCue("Battle_Theme_Base");
            bgmCategory = audioEngine.GetCategory("BGM");
            sfxCategory = audioEngine.GetCategory("SFX");

            c = new Character(this);
            bm = new BlockManager(this);
        }
            
        public override void Update(GameTime gameTime)
        {
            audioEngine.Update();
            bm.Update(gameTime);
            c.Update(gameTime);
            enemies.ForEach(e => e.Update(gameTime));
            for (int i = 0; i < enemies.Count; ++i)
            {
                enemies[i].Update(gameTime);
                if (!enemies[i].alive) enemies.RemoveAt(i);
            }
            if (Input.KeyPressed(Keys.D1)) enemies.Add(new Bird(this));
            if (Input.KeyPressed(Keys.D2)) enemies.Add(new Bunny(this));
            if (Input.KeyPressed(Keys.M)) { bgmVolume = bgmVolume == 0f ? 1f : 0f; bgmCategory.SetVolume(bgmVolume); }
            if (Input.KeyPressed(Keys.N)) { sfxVolume = sfxVolume == 0f ? 1f : 0f; sfxCategory.SetVolume(sfxVolume); }
        }
        
        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            bm.Draw(spriteBatch, gameTime);
            c.Draw(spriteBatch);
            enemies.ForEach(e => e.Draw(spriteBatch));
            bm.DrawForeground(spriteBatch, gameTime);


            // HUD
            spriteBatch.DrawString(Thaneros.Font, c.isEros ? "Eros" : "Thanatos", new Vector2(0, 0), Color.White);
            spriteBatch.DrawString(Thaneros.Font, "Char HP: " + c.HP, new Vector2(0, 30), Color.White);
            spriteBatch.DrawString(Thaneros.Font, "World HP: " + c.wHP, new Vector2(0, 60), Color.White);
            spriteBatch.DrawString(Thaneros.Font, "c.Speed: " + c.speed.X + " | " + c.speed.Y, new Vector2(0, 90), Color.White);
            spriteBatch.DrawString(Thaneros.Font, "Enemies: " + enemies.Count, new Vector2(0, 120), Color.White);
        }
    }
}